package com.example.demo.mvc_interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;


//@Component
//public class InterceptorTest extends HandlerInterceptorAdapter
//{
//    public InterceptorTest(){
//        System.out.println("Constructor: InterceptorTest");
//    }
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
//    {
//        System.out.println("preHandle: InterceptorTest");
//        if (handler instanceof HandlerMethod)
//        {
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            Method method = handlerMethod.getMethod();
//            System.out.println("preHandle: InterceptorTest. handler name: " + method.getName());
//            return true;
//        }
//        else
//        {
//            return super.preHandle(request, response, handler);
//        }
//    }
//}

public class InterceptorTest implements HandlerInterceptor
{
//    public InterceptorTest(){
//        System.out.println("Constructor: InterceptorTest");
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        System.out.println("preHandle: InterceptorTest");
        if (handler instanceof HandlerMethod)
        {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            System.out.println("preHandle: InterceptorTest. handler name: " + method.getName());
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}